package org.nmgyjt.springboot.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.nmgyjt.springboot.entity.admin.IDAASUserResponse;
import org.nmgyjt.springboot.entity.admin.UserInfo;
import org.nmgyjt.springboot.entity.exception.BizException;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

@Slf4j
public class UserInfoTask implements Callable<UserInfo> {


    private String openid;

    public UserInfoTask(String openid) {
        this.openid = openid;
    }

    @Override
    public UserInfo call() throws Exception {
        String token = NMGYJTIDAASTokenUtils.getToken();
        if(StringUtils.isEmpty(token))

        {
            log.info("get token exception >> 接口访问权限异常,请联系管理员");
            throw new BizException("接口访问权限异常,请联系管理员");
        }

        //log.info("getToken  >> success");
        HashMap<String, String> headerParams = new HashMap<>();
        Map<String, String> bodyParams = new HashMap<>();

        headerParams.put("Content-Type","application/json");
        headerParams.put("Authorization","bearer "+token);

        // log.info("token is >> {}", token);
        bodyParams.put("keywords",openid);
        bodyParams.put("object_type","USER");

        String str = HttpUtils.httpGet("http://idaas-admin.nmgyjtwg.org.cn/api/v1/tn-3f90f739227e498bb3505237e6c397bb/objects/search", bodyParams, headerParams);
        if(StringUtils.isNotEmpty(str))

        {
            IDAASUserResponse userResponse = JSONObject.parseObject(str, IDAASUserResponse.class);
            List<UserInfo> data = userResponse.getData();
            if (!CollectionUtils.isEmpty(data)) {
                return data.get(0);
            }
        }
        return null;
    }

}
